package com.autolinq.util.image;

import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * @author zxn
 * @version 创建时间：2014-7-2 上午11:40:40
 * 
 */
public class ImageUtils {
	
	public static boolean createImgFromURL(String imgurl,String fileName) {  
        boolean b=false;  
        try {  
            //实例化url  
            URL url = new URL(imgurl);  
            //载入图片到输入流  
            java.io.BufferedInputStream bis = new BufferedInputStream(url.openStream());  
            //实例化存储字节数组  
            byte[] bytes = new byte[100];  
            //设置写入路径以及图片名称  
            OutputStream bos = new FileOutputStream(new File(fileName));  
            int len;  
            while ((len = bis.read(bytes)) > 0) {  
                bos.write(bytes, 0, len);  
            }  
            bis.close();  
            bos.flush();  
            bos.close();  
            //关闭输出流  
            b=true;  
        } catch (Exception e) {  
            //如果图片未找到  
        	e.printStackTrace();
            b=false;  
        }  
        
       return b;  
  
    }  
  /**
   * 将网络图片进行Base64位编码
   * 
   * @param imgUrl
   *			图片的url路径，如http://.....xx.jpg
   * @return
   */
  public static String encodeImgageToBase64(URL imageUrl) {// 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
    ByteArrayOutputStream outputStream = null;
    try {
      BufferedImage bufferedImage = ImageIO.read(imageUrl);
      outputStream = new ByteArrayOutputStream();
      ImageIO.write(bufferedImage, "PNG", outputStream);
    } catch (MalformedURLException e1) {
      e1.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    // 对字节数组Base64编码
    BASE64Encoder encoder = new BASE64Encoder();
    return encoder.encode(outputStream.toByteArray());// 返回Base64编码过的字节数组字符串
  }

  /**
   * 将本地图片进行Base64位编码
   * 
   * @param imgUrl
   *			图片的url路径，如http://.....xx.jpg
   * @return
   */
  public static String encodeImgageToBase64(File imageFile) {// 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
    ByteArrayOutputStream outputStream = null;
    try {
      BufferedImage bufferedImage = ImageIO.read(imageFile);
      outputStream = new ByteArrayOutputStream();
      ImageIO.write(bufferedImage, "PNG", outputStream);
    } catch (MalformedURLException e1) {
      e1.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    // 对字节数组Base64编码
    BASE64Encoder encoder = new BASE64Encoder();
    return encoder.encode(outputStream.toByteArray());// 返回Base64编码过的字节数组字符串
  }

  /**
   * 将Base64位编码的图片进行解码，并保存到指定目录
   * 
   * @param base64
   *			base64编码的图片信息
   * @return
   */
  public static void decodeBase64ToImage(String base64, String path,
      String imgName) {
    BASE64Decoder decoder = new BASE64Decoder();
    try {
      FileOutputStream write = new FileOutputStream(new File(path
          + imgName));
      byte[] decoderBytes = decoder.decodeBuffer(base64);
      write.write(decoderBytes);
      write.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  
  
  public static void main(String[] args){
	  //File source = new File("C://00000575.3gp");
	  FileInputStream fis = null;
	try {
		fis = new FileInputStream("C://00000575.3gp");
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

	  BufferedInputStream bis=new BufferedInputStream(fis);

	  ByteArrayOutputStream baos=new ByteArrayOutputStream();

	  int c = 0;
	try {
		c = bis.read();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}//读取bis流中的下一个字节

	  while(c!=-1){

	       baos.write(c);

	       try {
			c=bis.read();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	  }

	  try {
		bis.close();
		} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	   
	    System.out.println( new String(org.apache.commons.codec.binary.Base64.encodeBase64(baos.toByteArray(),false)));// 返回Base64编码过的字节数组字符串
  }
}